//
//  IntelligenceVC.m
//  Project2
//
//  Created by hundredsandc on 2017/11/8.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "IntelligenceVC.h"
#import "intelligenceCell.h"
@interface IntelligenceVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArr;


@end

@implementation IntelligenceVC
{
    NSInteger page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"情报";
    page =1;
    [self setupUI];
    [self getData];
}
-(void)setupUI{
    [self.view addSubview:self.tableView];
}
-(void)getData{
//http://www.buyinball.com/app-web/api/info/qry_infos?page_size=10&info_type=1&app_version=1.2.7&channel_no=360&page_index=1
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page_size"]=@"20";
    params[@"info_type"]=@"1";
    params[@"app_version"]=@"1.2.7";
    params[@"channel_no"]=@"360";
    params[@"page_index"]=@(page);
    [HTTPManagers getWithRecommendURL:@"qry_infos" params:params success:^(id json) {
        if ([json[@"result_msg"]isEqualToString:@"成功"]) {
            NSDictionary *qry_infos = json[@"qry_infos"];
            NSArray *data = qry_infos[@"data"];
            self.dataArr = [data mutableCopy];
        }
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = RGBCOLOR(0xf4, 0xf4, 0xf4);
    return view;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.dataArr.count>0) {
        
    NSDictionary *dict = self.dataArr[indexPath.section];
    WEBViewController *web = [WEBViewController new];
    web.url = dict[@"info_url"];
    [self.navigationController pushViewController:web animated:YES];
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    intelligenceCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([intelligenceCell class]) forIndexPath:indexPath];
    
    if (self.dataArr.count>0) {
        NSDictionary *dict = self.dataArr[indexPath.section];
        NSString *titleStr = dict[@"info_desc"];
        titleStr = [titleStr stringByReplacingOccurrencesOfString:@"</span>" withString:@""];
        titleStr = [titleStr stringByReplacingOccurrencesOfString:@"<span>" withString:@""];
        [cell customTitle:dict[@"info_title"] titleDesc:titleStr source:dict[@"info_src"] time:dict[@"info_time"]];
    }
    return cell;
    
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([intelligenceCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([intelligenceCell class])];
    }
    return _tableView;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


@end
